namespace jQube.Web.UI.Controls
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using jQube.Web.UI;
    using jQube.Web.UI.Design;

    [RequiredScript("jQube.Resources.Controls.Accordion.jquery.accordion-1.0.0.js")]
    [RequiredStyleSheet("jQube.Resources.Controls.Accordion.Accordion.css")]
    [ParseChildren(true)]
    [PersistChildren(true)]
    [DefaultProperty("Panes")]
    [Designer(typeof(AccordionDesigner))]
    [ToolboxBitmap(typeof(Accordion), "jQube.Resources.Controls.Accordion.Accordion.ico")]
    public class Accordion : WebControl, IScriptHandledControl
    {
        private bool requireOpenedPanel;
        private int selectedIndex;
        private AccordionAutoSize autoSize;
        private AccordionPaneCollection panes;

        public Accordion()
            : base(HtmlTextWriterTag.Div)
        {
            this.CssClass = "jQube-accordion";
            this.AutoSize = AccordionAutoSize.None;
            this.selectedIndex = -1;
            this.requireOpenedPanel = false;
        }


        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public AccordionPaneCollection Panes
        {
            get
            {
                if (this.panes == null)
                    this.panes = new AccordionPaneCollection(this);

                return this.panes;
            }
        }

        [DefaultValue("jQube-accordion")]
        public override string CssClass
        {
            get
            {
                return base.CssClass;
            }
            set
            {
                base.CssClass = value;
            }
        }

        [DefaultValue(AccordionAutoSize.None)]
        public AccordionAutoSize AutoSize
        {
            get
            {
                return this.autoSize;
            }
            set
            {
                this.autoSize = value;
            }
        }

        [DefaultValue(-1)]
        public int SelectedIndex
        {
            get { return this.selectedIndex; }
            set { this.selectedIndex = value; }
        }

        [DefaultValue(false)]
        public bool RequireOpenedPanel
        {
            get { return this.requireOpenedPanel; }
            set { this.requireOpenedPanel = value; }
        }
        
        protected override void OnInit(EventArgs e)
        {
            ScriptManager scriptManager = ScriptManager.GetCurrent(base.Page);

            scriptManager.AddRequiredScript(typeof(Accordion));
            scriptManager.AddRequiredStyleSheet(typeof(Accordion));
            scriptManager.AddInitilizationScript(
                    string.Format("$(\"#{0}\").accordion({1});",
                        this.ClientID,
                        this.GetInitilizationOptions()));

            base.OnInit(e);
        }

        public string GetInitilizationOptions()
        {
            StringBuilder options = new StringBuilder();

            options.AppendFormat(
@"
            autoSize : {0},
            selectedIndex : {1},
            requireOpenedPanel : {2},
            panels  : {3}
",
              (int)this.AutoSize,
              this.SelectedIndex,
              this.RequireOpenedPanel.ToString().ToLower(),
              this.GetPanelsClientIDOptionsArray());

            options.Insert(0, " { ");
            options.Append("\t}");

            return options.ToString();
        }

        private string GetPanelsClientIDOptionsArray()
        {
            StringBuilder panels = new StringBuilder();

            if (this.Panes.Count == 0)
            {
                panels.Append("null");
            }
            else
            {
                panels.Append("[");

                foreach (AccordionPane pane in this.Panes)
                {
                    panels.AppendFormat("\"{0}\"", pane.ClientID);

                    if (this.Panes.IndexOf(pane) != this.Panes.Count - 1)
                    {
                        panels.Append(", ");
                    }
                }

                panels.Append("]");
            }

            return panels.ToString();
        }
    }
}
